package com.wuwei.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.wuwei.business.Constant.CarConstant;
import com.wuwei.business.Dto.CheckDto;
import com.wuwei.business.VO.BackVo;
import com.wuwei.business.domain.BusCar;
import com.wuwei.business.domain.BusRent;
import com.wuwei.business.mapper.BusCarMapper;
import com.wuwei.business.mapper.BusRentMapper;
import com.wuwei.business.service.BusRentService;
import com.wuwei.common.core.domain.model.LoginUser;
import com.wuwei.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wuwei.business.mapper.BusCheckMapper;
import com.wuwei.business.domain.BusCheck;
import com.wuwei.business.service.BusCheckService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 内容: SSM
 * 作者: weishao
 */
@Service
@Slf4j
public class BusCheckServiceImpl implements BusCheckService{

    @Resource
    private BusCheckMapper busCheckMapper;
    @Autowired
    private BusCarMapper busCarMapper;
    @Autowired
    private BusRentMapper busRentMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return busCheckMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BusCheck record) {
        return busCheckMapper.insert(record);
    }

    @Override
    public int insertSelective(BusCheck record) {
        return busCheckMapper.insertSelective(record);
    }

    @Override
    public BusCheck selectByPrimaryKey(Integer id) {
        return busCheckMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BusCheck record) {
        return busCheckMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BusCheck record) {
        return busCheckMapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean doBackCar(LoginUser loginUser, BusCheck busCheck) {
            log.info("还车{}，当前用户id为{}", JSON.toJSONString(busCheck), loginUser.getUserId());
        String username = loginUser.getUsername();
        busCheck.setCreateTime(new Date());
        busCheck.setCreateBy(username);
        busCheck.setUpdateTime(new Date());
        busCheck.setUpdateBy(username);
        BusRent busRent = busRentMapper.selectBusRentByrentSn(busCheck.getRentSn());
        BusCar busCar = busCarMapper.selectCarByCarNum(busRent.getCarNumber());
        busCheck.setDeptId(busCar.getDeptId());
        int i = busCheckMapper.insertSelective(busCheck);
        if (i>0){
            busRent.setRentStatus(CarConstant.CAR_REBACK);
            busRent.setUpdateTime(new Date());
            busRent.setUpdateBy(username);
            int a = busRentMapper.updateByPrimaryKey(busRent);
            if (a>0){
                busCar.setStatus(CarConstant.CAR_NOT_RENT);
                busCar.setUpdateTime(new Date());
                busCar.setUpdateBy(username);
                int b = busCarMapper.updateBusCar(busCar);
                if (b>0){
                   return true;
                }
            }
        }
            throw new ServiceException("汽车入库失败，请联系管理员");
    }

    @Override
    public List<BusCheck> selectCheckPage(LoginUser loginUser, CheckDto checkDto) {
        return busCheckMapper.selectCheckPage(loginUser,checkDto);
    }
}

